package com.orange.eduservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.orange.commonutils.result.Result;
import com.orange.commonutils.vo.EduCourseVo;
import com.orange.eduservice.entity.EduCourse;
import com.orange.eduservice.entity.vo.CourseInfoForm;
import com.orange.eduservice.entity.vo.CoursePublishVo;
import com.orange.eduservice.entity.vo.CourseQuery;
import com.orange.eduservice.entity.vo.front.CourseWebVo;
import com.orange.eduservice.service.EduCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author orange
 * @since 2021-12-17
 */
@Api(tags = "课程管理")
@RestController
@RequestMapping("/eduService/edu-course")
@CrossOrigin //解决跨域问题
public class EduCourseController {

    @Autowired
    private EduCourseService eduCourseService;

    //添加课程基本信息方法
    @ApiOperation("添加课程基本信息")
    @PostMapping("/addCourseInfo")
    public Result addCourseInfo(@ApiParam("课程基本信息对象") @RequestBody CourseInfoForm courseInfoForm) {
        //返回添加之后课程id，为了后面添加大纲使用
        String id = eduCourseService.saveCourseInfo(courseInfoForm);
        return Result.ok().data("courseId", id);
    }

    //根据课程id查询课程基本信息
    @ApiOperation("根据课程id查询基本信息")
    @GetMapping("/getCourseInfoById/{courseId}")
    public Result getCourseInfoById(@ApiParam("课程id") @PathVariable String courseId) {
        CourseInfoForm courseInfoForm = eduCourseService.getCourseInfo(courseId);
        return Result.ok().data("courseInfoForm", courseInfoForm);
    }

    //修改课程信息
    @ApiOperation("修改课程信息")
    @PostMapping("/updateCourseInfo")
    public Result updateCourseInfo(@ApiParam("课程基本信息对象") @RequestBody CourseInfoForm courseInfoForm) {
        eduCourseService.updateCourseInfo(courseInfoForm);
        return Result.ok();
    }

    //根据课程id查询课程确认信息
    @ApiOperation("根据课程id查询课程确认信息")
    @GetMapping("/getPublishCourseInfo/{id}")
    public Result getPublishCourseInfo(@ApiParam("课程id") @PathVariable String id) {
        CoursePublishVo publishCourseInfo = eduCourseService.getPublishCourseInfo(id);
        return Result.ok().data("publishCourse", publishCourseInfo);
    }

    //课程最终发布
    @ApiOperation("课程最终发布")
    @PostMapping("publishCourse/{id}")
    public Result publishCourse(@ApiParam("课程id") @PathVariable String id) {
        EduCourse eduCourse = new EduCourse();
        //设置课程发布状态 (Normal<==>已发布，Draft<==>未发布)
        eduCourse.setStatus("Normal");
        eduCourse.setId(id);
        boolean flag = eduCourseService.updateById(eduCourse);
        if (flag) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    //课程列表 基本实现
    @ApiOperation("查询所有课程")
    @GetMapping("/getCourseList")
    public Result getCourseList() {
        List<EduCourse> list = eduCourseService.list(null);
        return Result.ok().data("list", list);
    }

    //多条件查询课程带分页
    @ApiOperation(value = "多条件查询课程带分页")
    @PostMapping("/pageCourseCondition/{page}/{limit}")
    public Result pageCourseCondition(@ApiParam(name = "page", value = "当前页码", required = true) @PathVariable Long page,
                                      @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable Long limit,
                                      @ApiParam("查询条件\r\r\tstatus:发布状态\r\r\ttitle:课程名称") @RequestBody(required = false) CourseQuery courseQuery) {
        //通过封装courseQuery对象来直接传递查询条件
        //创建分页page对象
        Page<EduCourse> pageParam = new Page<>(page, limit);
        //调用方法实现多条件分页查询
        eduCourseService.pageQuery(pageParam, courseQuery);
        //获取查询到的数据
        List<EduCourse> records = pageParam.getRecords();
        //获取总记录数
        long total = pageParam.getTotal();
        return Result.ok().data("total", total).data("rows", records);
    }

    //课程列表中删除课程方法
    @ApiOperation("根据课程id删除课程")
    @DeleteMapping("/removeCourseById/{id}")
    public Result removeCourseById(@ApiParam("课程id") @PathVariable String id) {
        boolean flag = eduCourseService.removeCourse(id);
        if (flag) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    //根据课程id，查询课程信息【订单】
    @PostMapping("/getCourseInfoByIdOrder/{courseId}")
    public EduCourseVo getCourseInfoByIdOrder(@PathVariable String courseId){
        CourseWebVo courseInfo = eduCourseService.getBaseCourseInfo(courseId);
        EduCourseVo eduCourseVo = new EduCourseVo();
        BeanUtils.copyProperties(courseInfo,eduCourseVo);

        return eduCourseVo;
    }
}

